﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.SessionState;

namespace HZ.Supervise.Web.Meet
{
    /// <summary>
    /// GetPara 的摘要说明
    /// </summary>
    public class GetPara : IHttpHandler, IRequiresSessionState
    {
        IBLL.IBLL_ProcureCatalog ihc = new HZ.Supervise.BLL.BLL_ProcureCatalog();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string Type = context.Request["type"];
            string mid = context.Request["mid"];
            string areaid = CurrentAccount.GetUser().Org.Area.AreaID;
            if (Type == "get")
            {
                DataTable dt = ihc.GetHosList(string.Format("and AreaID like '{0}%'", areaid.Substring(0, 4)), mid);
                List<HZ.Supervise.Model.Hospital> list = new List<HZ.Supervise.Model.Hospital>();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    HZ.Supervise.Model.Hospital h = new HZ.Supervise.Model.Hospital();
                    h.HospitalID = dt.Rows[i]["HID"].ToString();
                    h.HospitalName = dt.Rows[i]["HName"].ToString();
                    list.Add(h);
                }
                context.Response.Write(new JavaScriptSerializer().Serialize(list));
            }
            else if (Type == "save")
            {
                //反序列化json   
                System.IO.Stream stream = context.Request.InputStream;
                System.IO.StreamReader sr = new System.IO.StreamReader(stream, System.Text.Encoding.GetEncoding("UTF-8"));
                string sJson = sr.ReadToEnd();
                sJson = HttpUtility.UrlDecode(sJson);//js传值编码包含%的值，此处解码

                if (sJson.Contains("&"))
                {
                    string[] sArr = sJson.Split('&');

                    string ids = sArr[0].Split('=')[1];//页面行号 1,2,3...
                    string[] idstr = ids.Split(',');
                    Dictionary<string, string> dic = new Dictionary<string, string>();
                    for (int i = 1; i < sArr.Length; i++)//从1开始，0是ids，上面已获取
                    {
                        string[] sArr1 = sArr[i].Split('=');
                        dic.Add(sArr1[0], sArr1[1]);
                    }

                    List<Model.MeetInfo> list = new List<Model.MeetInfo>();
                    for (int i = 0; i < idstr.Length; i++)
                    {
                        HZ.Supervise.Model.MeetInfo mt = new Model.MeetInfo();
                        mt.MID = mid;
                        mt.JdjgID = CurrentAccount.GetUser().Org.OrgID;
                        mt.CHAreaID = areaid;
                        mt.HID = dic["hd" + idstr[i]];
                        mt.HosName = dic["hm" + idstr[i]];
                        mt.Name = dic["xm" + idstr[i]];
                        mt.XB = dic["xb" + idstr[i]];
                        mt.Postion = dic["zw" + idstr[i]];
                        mt.TelNo = dic["dh" + idstr[i]];
                        mt.IsZS = dic["zs" + idstr[i]];
                        list.Add(mt);
                    }
                    if (list.Count > 0)
                    {
                        int sb = ihc.GetHaveSubCount(CurrentAccount.GetUser().Org.OrgID, mid);//已上报人数
                        if (90 - sb > list.Count)
                        {
                            int flag = ihc.SaveHosMeet(list);
                            context.Response.Write(new JavaScriptSerializer().Serialize(flag));
                        }
                        else
                        {
                            context.Response.Write(new JavaScriptSerializer().Serialize(9));
                        }
                    }
                }
            } 
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}